package bsf.baseservice.businessmq.systemruntime;

import bsf.base.BsfException;
import bsf.util.DateUtil;
import org.apache.commons.lang3.StringUtils;

/**
 分区规则帮助类
 
*/
public class PartitionRuleHelper
{
	/** 
	 分区命名规则
	 
	 @param partitionid
	 @return 
	*/
	public static String partitionNameRule(int partitionid)
	{

		return StringUtils.leftPad(partitionid + "",2, '0');
	}
	/** 
	 获取消息表名
	 
	 @param tablepartition
	 @param day
	 @return 
	*/
	public static String getTableName(int tablepartition, java.util.Date day)
	{
		return "tb_messagequeue_" + partitionNameRule(tablepartition) + "_" + DateUtil.toString(day,"yyMMdd");
	}
	/** 
	 根据消息表名获取隐藏的规则信息
	 
	 @param tablename
	 @return 
	*/
	public static TableInfo getTableInfo(String tablename)
	{
		String partition = tablename.replace("tb_messagequeue_", "");
		String[] partitioninfo = partition.split("[_]", -1);
		TableInfo tempVar = new TableInfo();
		tempVar.TablePartition = Integer.parseInt(partitioninfo[0]);
		tempVar.Day=DateUtil.parse(partitioninfo[1], "yyMMdd");
		return tempVar;
	}
	/** 
	 获取数据节点名
	 
	 @param datanodepartition
	 @return 
	*/
	public static String getDataNodeName(int datanodepartition)
	{
		return SystemParamConfig.DataNode_DataBaseName_Prefix + partitionNameRule(datanodepartition);
	}

	/** 
	 获取MQ消息ID 消息id号,规则1+数据节点编号+表分区编号+时间分区号+自增id
	 
	 @param info
	 @return 
	*/
	public static long getMQID(MQIDInfo info)
	{
		String id = "1" + partitionNameRule(info.DataNodePartition) + partitionNameRule(info.TablePartition) + DateUtil.toString( info.Day,"yyMMdd") + StringUtils.leftPad(info.AutoID+"",8, '0');
		return Long.parseLong(id);
	}
	/** 
	 获取MQID隐藏的信息
	 
	 @param id
	 @return 
	*/
	public static MQIDInfo getMQIDInfo(long id)
	{
		MQIDInfo info = new MQIDInfo();
		String example = "1010115062900000000";
		String strid = (new Long(id)).toString();
		if(strid.length()!=example.length())
		{
			throw new BsfException("消息Id格式不正确:"+id,PartitionRuleHelper.class);
		}
		info.DataNodePartition=Integer.parseInt(strid.substring(1, 3));
		info.TablePartition=Integer.parseInt(strid.substring(3, 5));
		info.Day=DateUtil.parse(strid.substring(5, 11), "yyMMdd");
		info.AutoID=Integer.parseInt(strid.substring(11, 19));
		return info;
	}
	/** 
	 获取分区id号  分区id号,规则1+数据节点编号+表分区编号
	 
	 @param info
	 @return 
	*/
	public static int getPartitionID(PartitionIDInfo info)
	{
		//PartitionIDInfo info = new PartitionIDInfo();string example = "10101";string strid = id.ToString();
		String id = "1" + partitionNameRule(info.DataNodePartition) + partitionNameRule(info.TablePartition);
		return Integer.parseInt(id);
	}
	/** 
	 获取分区id号隐藏的信息
	 
	 @param id
	 @return 
	*/
	public static PartitionIDInfo getPartitionIDInfo(int id)
	{
		PartitionIDInfo info = new PartitionIDInfo();
		String example = "10101";
		String strid = (new Integer(id)).toString();
		if(strid.length()!=example.length())
		{
			throw new BsfException("分区Id格式不正确:"+id,PartitionRuleHelper.class);
		}
		info.DataNodePartition=Integer.parseInt(strid.substring(1, 3));
		info.TablePartition=Integer.parseInt(strid.substring(3, 5));
		return info;
	}
}